<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/6/22 0022
 * Time: 13:53
 */

namespace app\admin\controller;

use think\Request;


class Statistics extends BaseController
{
    protected  $_pvModel;

    protected  $_ipModel;

    public function __construct(Request $request = null){

        parent::__construct($request);

        $this->_pvModel = new  \app\model\model\Accesspv();

        $this->_ipModel = new  \app\model\model\Accessip();
    }

    public function accesspv(){
        if(Request::instance()->post()) {

            $data = $this->_pvModel->getListPage(false, input('limit'));

            $list = $data->toArray();

            echo json_echo($list);exit;
        } else {
            $this->assign('params', $this->_statisticsPv());
            return $this->fetch();
        }
    }

    public function removeaccesspv(){
        $this->_pvModel->where('1=1')->delete();
        return json_echo_msg(200,'访问明细清除成功！');
    }

    public function accesspv_chart(){
        if(Request::instance()->post()) {
            $type = input('type', 30);
            $params = [];
            for ($i = $type; $i >= 1; $i --){
                $params[date("Y-m-d", strtotime("-$i day"))] = 0;
            }
            $start_time = strtotime(date('Y-m-d', strtotime("-$type day")));
            $end_time = strtotime(date('Y-m-d')) -1;

            $pv_list = $this->_pvModel->where(['create_time'=>['between', [$start_time, $end_time]]])->column('id,create_time,ip');
            if(!empty($pv_list)){
                foreach ($pv_list as $val){
                    $params[date('Y-m-d', $val['create_time'])] += 1;
                }
            }
            $res = [
                'days'  => array_keys($params),
                'data'  => array_values($params)
            ];
            return json_echo_msg(200,'访问明细清除成功！', $res);
        } else {

            $this->assign('params', $this->_statisticsPv());
        }
        return $this->fetch();
    }

    public function accessip(){
        if(Request::instance()->post()) {

            $data = $this->_ipModel->getListPage(false, input('limit'));

            $list = $data->toArray();

            echo json_echo($list);exit;
        } else {
            $this->assign('params', $this->_statisticsIp());
            return $this->fetch();
        }
    }

    public function removeaccessip(){
        $this->_ipModel->where('1=1')->delete();
        return json_echo_msg(200,'访问明细清除成功！');
    }

    public function accessip_chart(){
        if(Request::instance()->post()) {
            $type = input('type', 30);
            $params = [];
            for ($i = $type; $i >= 1; $i --){
                $params[date("Y-m-d", strtotime("-$i day"))] = 0;
            }
            $start_time = strtotime(date('Y-m-d', strtotime("-$type day")));
            $end_time = strtotime(date('Y-m-d')) -1;

            $ip_list = $this->_ipModel->where(['create_time'=>['between', [$start_time, $end_time]]])->column('id,create_time,ip');
            if(!empty($ip_list)){
                foreach ($ip_list as $val){
                    $params[date('Y-m-d', $val['create_time'])] += 1;
                }
            }
            $res = [
                'days'  => array_keys($params),
                'data'  => array_values($params)
            ];
            return json_echo_msg(200,'访问明细清除成功！', $res);
        } else {

            $this->assign('params', $this->_statisticsIp());
        }
        return $this->fetch();
    }

    public function _statisticsPv(){
        $params = ['total'=>0,'month'=>0,'last_month'=>0,'day'=>0, 'yesterday'=>0,'week'=>0,'last_week'=>0,'contrast_week'=>0];
        $params['total'] = $this->_pvModel->count();
        $params['day'] = $this->_pvModel->whereTime('create_time', 'today')->count();
        $params['yesterday'] = $this->_pvModel->whereTime('create_time', 'yesterday')->count();
        $params['month'] = $this->_pvModel->whereTime('create_time', 'month')->count();
        $params['last_month'] = $this->_pvModel->whereTime('create_time', 'last month')->count();
        if(empty($params['month'])){
            $params['contrast_month'] = 0;
        } else {
            $params['contrast_month'] = round((($params['month'] - $params['last_month']) / $params['month'])*100, 2);
        }


        $params['week'] = $this->_pvModel->whereTime('create_time', 'week')->count();
        $params['last_week'] = $this->_pvModel->whereTime('create_time', 'last week')->count();
        if(empty($params['week'])){
            $params['contrast_week'] = 0;
        } else {
            $params['contrast_week'] = round((($params['week'] - $params['last_week']) / $params['week'])*100, 2);
        }
        return $params;
    }

    public function _statisticsIp(){
        $params = ['total'=>0,'month'=>0,'last_month'=>0,'day'=>0, 'yesterday'=>0,'week'=>0,'last_week'=>0,'contrast_week'=>0];
        $params['total'] = $this->_ipModel->count();
        $params['day'] = $this->_ipModel->whereTime('create_time', 'today')->count();
        $params['yesterday'] = $this->_ipModel->whereTime('create_time', 'yesterday')->count();
        $params['month'] = $this->_ipModel->whereTime('create_time', 'month')->count();
        $params['last_month'] = $this->_ipModel->whereTime('create_time', 'last month')->count();
        if(empty($params['month'])){
            $params['contrast_month'] = 0;
        } else {
            $params['contrast_month'] = round((($params['month'] - $params['last_month']) / $params['month'])*100, 2);
        }


        $params['week'] = $this->_ipModel->whereTime('create_time', 'week')->count();
        $params['last_week'] = $this->_ipModel->whereTime('create_time', 'last week')->count();
        if(empty($params['week'])){
            $params['contrast_week'] = 0;
        } else {
            $params['contrast_week'] = round((($params['week'] - $params['last_week']) / $params['week'])*100, 2);
        }
        return $params;
    }
}